package com.app.bimo.commonbusiness.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.getkeepsafe.relinker.ReLinker;
import com.tencent.mmkv.MMKV;

import java.util.HashSet;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by newbiechen on 17-4-16.
 */

public class SharedPreUtils {


    private static SharedPreUtils sInstance;
    private MMKV mmkv;
//    private SharedPreferences.Editor sharedWritable;

    private SharedPreUtils(final Context context) {

        String dir = context.getFilesDir().getAbsolutePath() + "/IReader_pref";
        String rootDir = MMKV.initialize(dir, new MMKV.LibLoader() {
            @Override
            public void loadLibrary(String libName) {
                ReLinker.loadLibrary(context, libName);
            }
        });
        mmkv = MMKV.defaultMMKV();
    }

    public static SharedPreUtils getInstance(Context context) {
        if (sInstance == null) {
            synchronized (SharedPreUtils.class) {
                if (sInstance == null) {
                    sInstance = new SharedPreUtils(context);
                }
            }
        }
        return sInstance;
    }

    private MMKV getMmkv() {
        return mmkv;
    }

    public String getString(String key) {
        return mmkv.decodeString(key, "");
    }

    public void putString(String key, String value) {
        mmkv.encode(key, value);
    }

    public void putInt(String key, int value) {
        mmkv.encode(key, value);

    }

    public void putBoolean(String key, boolean value) {
        mmkv.encode(key, value);

    }

    public void putLong(String key, long value) {
        mmkv.encode(key, value);

    }

    public int getInt(String key, int def) {
        return mmkv.decodeInt(key, def);
    }

    public long getLong(String key) {
        return mmkv.decodeLong(key);
    }

    public boolean getBoolean(String key, boolean def) {
        return mmkv.decodeBool(key, def);
    }

    /**
     * 数据迁移
     * 迁移完成以后  旧的存在SharedPreferences 的数据全部清除了
     * 这个需要在程序运行之前开始  只迁移过一次即可
     *
     * @param context
     */
    public void changeData(Context context) {
        MMKV preferences = SharedPreUtils.getInstance(context).getMmkv();
        // 迁移旧数据
        {
            SharedPreferences old_man = context.getSharedPreferences("IReader_pref", MODE_PRIVATE);
            preferences.importFromSharedPreferences(old_man);
            old_man.edit().clear().commit();
        }
        // 跟以前用法一样
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("bool", true);
        editor.putInt("int", Integer.MIN_VALUE);
        editor.putLong("long", Long.MAX_VALUE);
        editor.putFloat("float", -3.14f);
        editor.putString("string", "hello, imported");
        HashSet<String> set = new HashSet<String>();
        set.add("W");
        set.add("e");
        set.add("C");
        set.add("h");
        set.add("a");
        set.add("t");
        editor.putStringSet("string-set", set);
    }
}
